package com.qyhl.school.school.column.detail;

import com.qyhl.school.common.SchoolUrl;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.entity.school.SchoolDetailBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class SchoolColumnDetailModel implements SchoolColumnDetailContract.SchoolColumnDetailModel {
    private SchoolColumnDetailPresenter mPresenter;

    public SchoolColumnDetailModel(SchoolColumnDetailPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getDetail(int schoolId) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(SchoolUrl.BASE_SCHOOL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.SCHOOL_VERSION);
            object.put("name", "schoolDetail");
            object.put("schoolId", schoolId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<SchoolDetailBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setError("获取出错，请稍后再试！");
                    }

                    @Override
                    public void onSuccess(SchoolDetailBean bean) {
                        if (bean != null) {
                            mPresenter.setDetail(bean);
                        } else {
                            mPresenter.setError("暂无任何内容！");
                        }
                    }
                });
    }
}
